package com.dllo.lanou.cookapp.fragment;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.activity.ShowCookActivity;
import com.dllo.lanou.cookapp.adapter.ShowCookAdapter;
import com.dllo.lanou.cookapp.entity.Post;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by xijinhua on 2016/3/12.
 */
public class ShowCookFragment extends Fragment implements View.OnClickListener {
@Bind(R.id.pull_to_refresh_listview)
 PullToRefreshListView listview;
    List<String> imageUris;
    @Bind(R.id.send)
    ImageView send;
    private List<Post> list;
    private ShowCookAdapter adapter;
    private Handler handler;
     private boolean isRefresh= true;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_show,null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        imageUris = new ArrayList<>();
        send.setOnClickListener(this);

        adapter = new ShowCookAdapter(getActivity());
        handler= new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what ==0){
                    list = new ArrayList<>();

                    list = (List<Post>) msg.obj;


                    adapter.addData(list);
                }

            }
        };

        queryData();
       Log.i("list", list + "");
        //adapter = new ShowCookAdapter(getActivity());

      listview.setAdapter(adapter);
        listview.setMode(PullToRefreshBase.Mode.BOTH);//允许上拉下落
        listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                new LoadDataAsyncTask().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });

    }
public  void queryData(){
    BmobQuery<Post> query = new BmobQuery<Post>();
    query.setLimit(20);
    query.include("author");// 希望在查询帖子信息的同时也把发布人的信息查询出来
    query.findObjects(getActivity(), new FindListener<Post>() {
        @Override
        public void onSuccess(List<Post> lists) {
            Log.i("我得到的数据list", lists + "");
            Collections.reverse(lists);
            Message msg = new Message();
            msg.what = 0;
            msg.obj = lists;
            handler.sendMessage(msg);


            Log.i("我得到的数据list", lists + "");
        }

        @Override
        public void onError(int i, String s) {

        }
    });


}

    @Override
    public void onClick(View v) {
        switch (v.getId()){
           case R.id.send:

               Intent intent = new Intent (getActivity(), ShowCookActivity.class);
               startActivityForResult(intent, 101);
            break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 101:
                    if (data != null){

                    }

                break;
        }

    }




///监听事件



    class LoadDataAsyncTask extends AsyncTask<Void,Void,String>{


        @Override
        protected String doInBackground(Void... params) {

            //isRefresh = false;
            try {
                Thread.sleep(2000);
                queryData();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "success";
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            if ("success".equals(s)){

                listview.onRefreshComplete();//刷新完成
            }
        }
    }


}
